package org.example.cake.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.example.cake.entity.Buycar;

import java.util.List;


import java.util.List;
import java.util.Map;

@Mapper
public interface BuycarMapper {
    int deleteByPrimaryKey(Integer buyid);

    int insert(Buycar record);

    int insertSelective(Buycar record);

    Buycar selectByPrimaryKey(Integer buyid);

    int updateByPrimaryKeySelective(Buycar record);

    int updateByPrimaryKey(Buycar record);
    /**
     * 新增购物车
     * @param buycar 购物车信息
     * @Date 2025-07-31 谭丽娜
     * @return
     */
    @Insert("insert into buycar(userid,cakeid,cakesum,`type`) values (#{buycar.userid},#{buycar.cakeid},#{buycar.cakesum},#{buycar.type})")
    Integer addBuyCarapi(@Param("buycar") Buycar buycar);
    /**
     * 查询购物车以及结算信息
     * @Date 2025-07-31 谭丽娜
     * @param userid 用户id
     * @return
     */
    @Select("select * from buycar where userid=#{userid} ")
    List<Buycar> selectBuycarListapi(@Param("userid") Integer userid);
    /**
     * 修改购物车数量
     * @param buycar
     * @Date 2025-07-31 谭丽娜
     * @return
     */
    @Update("update buycar set cakesum=#{buycar.cakesum}  where buyid=#{buycar.buyid} ")
    Integer updateCountapi(@Param("buycar") Buycar buycar);
    /**
     * 删除购物车
     * @param buyid
     * @Date 2025-07-31 谭丽娜
     * @return
     */
    @Delete("delete from buycar where buyid=#{buyid} ")
    Integer deleteBuyapi(@Param("buyid") Integer buyid);
    /**
     * 删除所有的购物车
     * @Date 2025-07-31 谭丽娜
     * @param userid 用户id
     * @return
     */
    @Delete("delete from buycar where userid=#{userid} ")
    Integer deleteBuyAllapi(Integer userid);

    /**
     * 查询购物车详情（包含用户名称和蛋糕名称）
     * @return 购物车详情列表
     */
    List<Map<String, Object>> selectBuycarDetails();


    /**
     * 带分页的模糊查询（根据用户名或蛋糕名）
     * @param pageSize
     * @param searchKey
     * @return
     */
    List<Map<String, Object>> selectBuycarDetailsBySearch(
            @Param("searchKey") String searchKey,
            @Param("pageSize") Integer pageSize,
            @Param("offset") Integer offset
    );


    /**
     * 统计符合搜索条件的总记录数
     * @param searchKey
     * @return
     */
    Integer countBuycarDetailsBySearch(@Param("searchKey") String searchKey);

    /**
     * 根据用户ID查询购物车详情
     * @param userId 用户ID
     * @return 购物车详情列表
     */
    List<Map<String, Object>> selectBuycarDetailsByUserId(Integer userId);

    /**
     * 根据蛋糕ID查询购物车详情
     * @param cakeId 蛋糕ID
     * @return 购物车详情列表
     */
    List<Map<String, Object>> selectBuycarDetailsByCakeId(Integer cakeId);

    /**
     * 根据用户ID和蛋糕ID查询购物车详情
     * @param userId 用户ID
     * @param cakeId 蛋糕ID
     * @return 购物车详情列表
     */
    List<Map<String, Object>> selectBuycarDetailsByUserAndCake(@Param("userId") Integer userId,
                                                               @Param("cakeId") Integer cakeId);

    /**
     * 新增购物车记录
     * @param buycar 购物车实体
     * @return 受影响的行数
     */
    int insertBuycar(Buycar buycar);

    /**
     * 更新购物车记录
     * @param buycar 购物车实体
     * @return 受影响的行数
     */
    int updateBuycar(Buycar buycar);

    /**
     * 删除购物车记录
     * @param buyId 购物车ID
     * @return 受影响的行数
     */
    int deleteBuycar(Integer buyId);

    /**
     * 根据用户ID、蛋糕ID和尺寸类型获取购物车记录
     * @param userId 用户ID
     * @param cakeId 蛋糕ID
     * @param type 尺寸类型(0:4寸, 1:6寸)
     * @return 购物车实体
     */
    Buycar getBuycarByUserAndCakeAndType(@Param("userId") Integer userId,
                                         @Param("cakeId") Integer cakeId,
                                         @Param("type") Integer type);

    /**
     * 批量删除购物车记录
     * @param buyIds 购物车ID列表
     * @return 删除成功的数量
     */
    int batchDeleteBuycar(@Param("buyIds") List<Integer> buyIds);
    /**
     * 清楚购物车
     * @param buycarid 购物车id
     * @return
     */
    @Delete("delete from buycar where buyid=#{buycarid} ")
    Integer delBuycar(@Param("buycarid") int buycarid);

    /**
     * 查看购物车是否存在此蛋糕
     * @param buycar
     * @return
     */
    @Select("select COUNT(*) from buycar where cakeid=#{buycar.cakeid}")
    Integer selectBuyCarifhas(@Param("buycar") Buycar buycar);
    /**
     * 查询购物车蛋糕数量
     * @param buycar
     * @return
     */
    @Select("select cakesum from buycar where cakeid=#{buycar.cakeid} ")
    Integer selectcakecount(@Param("buycar")Buycar buycar);
    /**
     * 修改购物车信息
     * @param buycar
     * @return
     */
    @Update("update buycar set cakesum=#{buycar.cakesum}  where cakeid=#{buycar.cakeid} ")
    Integer updatebuycarCount(@Param("buycar")Buycar buycar);
    /**
     * 根据cakeid查询购物车里的总数
     * @Date 2025-08-11 谭丽娜
     * @param cakeid
     * @return
     */
    @Select("select cakesum from buycar where cakeid=#{cakeid} ")
    Integer cakecount(@Param("cakeid") Integer cakeid);
    /**
     * 根据蛋糕id修改购物车数量
     * @param buycar
     * @return
     */
    @Update("update buycar set cakesum=#{buycar.cakesum}  where cakeid=#{buycar.cakeid}  ")
    Integer updateCountCakeid(@Param("buycar") Buycar buycar);
}